<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\facade\Db;

class Dashboard extends Backend
{
    public function initialize (): void
    {
        parent::initialize();
    }

    public function index (): void
    {
        $where = [];
        $orderWhere = [];
        $technicianWhere = [];
        $extendWhere = [];

        if ($this->auth->isSuperAdmin()) {
            $where[] = ['type', '=', 1];
            $where[] = ['login_id', '=', 1];


            #代理商收入排名
            $agentSort = Db::name('giveaway_log')
                ->alias('l')
                ->join('ba_admin a ', 'a.id = l.login_id')
                ->where('l.type', 2)
                ->field('l.login_id, sum(l.money) as total, a.nickname,a.id,a.mobile,a.avatar')
                ->group('l.login_id')
                ->order('total desc')
                ->limit(10)
                ->select();
            if (!empty($agentSort)) {
                $agentSort = $agentSort->toArray();
                foreach ($agentSort as $k => $v) {
                    $agentSort[$k]['total'] = sprintf('%.2f', $v['total']);
                    $agentSort[$k]['sort'] = $k + 1;
                    $agentSort[$k]['avatar'] = config('buildadmin.cdn_url') . config('buildadmin.default_avatar');
                }
                unset($v);
            }


        } else {
            $where[] = ['type', '=', 2];
            $where[] = ['login_id', '=', $this->auth->id];
            $orderWhere[] = ['area_id', '=', $this->auth->bind_city_id];
            $technicianWhere[] = ['t.area_id', '=', $this->auth->bind_city_id];
            $extendWhere[] = ['u.extend_area_id', '=', $this->auth->bind_city_id];
        }
        #总收益
        $totalEarnings = Db::name('giveaway_log')->where($where)->sum('money');
        #今日收益
        $toDayTotalEarnings = Db::name('giveaway_log')->where($where)->whereTime('create_time', 'today')->sum('money');
        #总订单金额
        $orderPrice = \app\admin\model\Order::where($orderWhere)->sum('service_total_price');
        #已完成订单金额
        $orderCompletedPrice = \app\admin\model\Order::where('order_state', 'Completed')->where($orderWhere)->sum('service_total_price');


        #订单
        #已完成订单金额
        $orderTotalNum = \app\admin\model\Order::where($orderWhere)->count('id');
        $orderTodayTotalNum = \app\admin\model\Order::where($orderWhere)->whereTime('create_time', 'today')->count('id');
        $orderTodayCompletedTotalNum = \app\admin\model\Order::where($orderWhere)->whereTime('create_time', 'today')->where('order_state', 'Completed')->count('id');
        $orderTodayServiceTotalNum = \app\admin\model\Order::where($orderWhere)->whereIn('order_state', ['Start', 'tchEnd'])->count('id');

        #技师
        $technicianTotal = \app\admin\model\Technician::where($orderWhere)->count('technician_id');
        $technicianWorkTotal = \app\admin\model\Technician::where($orderWhere)->where('is_go_work', 1)->count('technician_id');
        $technicianNotWorkTotal = \app\admin\model\Technician::where($orderWhere)->where('is_go_work', 2)->count('technician_id');
        $technicianServiceTotal = \app\admin\model\Order::where($orderWhere)->whereIn('order_state', ['Start', 'tchEnd'])->group('technician_id')->count('technician_id');


        #技师收入排名
        $technicianSort = Db::name('giveaway_log')
            ->alias('l')
            ->join('ba_technician t ', 't.technician_id = l.login_id')
            ->where('l.type', 3)
            ->where($technicianWhere)
            ->field('l.login_id, sum(l.money) as total, t.technician_name,t.technician_id,t.tel,t.avatar')
            ->group('l.login_id')
            ->order('total desc')
            ->limit(10)
            ->select();
        if (!empty($technicianSort)) {
            $technicianSort = $technicianSort->toArray();
            foreach ($technicianSort as $k => $v) {
                $technicianSort[$k]['total'] = sprintf('%.2f', $v['total']);
                $technicianSort[$k]['sort'] = $k + 1;
                $technicianSort[$k]['avatar'] = config('buildadmin.cdn_url') . $v['avatar'];
            }
            unset($v);
        }


        #推广员收入排名
        $extendSort = Db::name('giveaway_log')
            ->alias('l')
            ->join('ba_user u ', 'u.id = l.login_id')
            ->where($extendWhere)
            ->where('l.type', 5)
            ->field('l.login_id, sum(l.money) as total, u.nickname,u.id,u.mobile,u.avatar')
            ->group('l.login_id')
            ->order('total desc')
            ->limit(10)
            ->select();
        if (!empty($extendSort)) {
            $extendSort = $extendSort->toArray();
            foreach ($extendSort as $k => $v) {
                $extendSort[$k]['total'] = sprintf('%.2f', $v['total']);
                $extendSort[$k]['sort'] = $k + 1;
            }
            unset($v);
        }

        $this->success('', [
            'totalEarnings'               => sprintf('%.2f', $totalEarnings),
            'toDayTotalEarning'           => sprintf('%.2f', $toDayTotalEarnings),
            'orderPrice'                  => sprintf('%.2f', $orderPrice),
            'orderCompletedPrice'         => sprintf('%.2f', $orderCompletedPrice),
            'orderTotalNum'               => $orderTotalNum,
            'orderTodayTotalNum'          => $orderTodayTotalNum,
            'orderTodayCompletedTotalNum' => $orderTodayCompletedTotalNum,
            'orderTodayServiceTotalNum'   => $orderTodayServiceTotalNum,
            'technicianTotal'             => $technicianTotal,
            'technicianWorkTotal'         => $technicianWorkTotal,
            'technicianNotWorkTotal'      => $technicianNotWorkTotal,
            'technicianServiceTotal'      => $technicianServiceTotal,
            'agentSort'                   => $agentSort ?? [],
            'extendSort'                  => $extendSort ?? [],
            'technicianSort'              => $technicianSort,

            'remark' => get_route_remark()
        ]);
    }
}